package com.ibs.hadoop.hbase.service.impl;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.ibs.hadoop.hbase.bean.QueryBean;
import com.ibs.hadoop.hbase.bean.UseComplaintBaseClass;
import com.ibs.hadoop.hbase.console.PropertiesEnumKey;
import com.ibs.hadoop.hbase.console.UserComplaintFlow;
import com.ibs.hadoop.hbase.service.UserComplaintService;
import com.ibs.hadoop.hbase.service.complaint.UserComplaintDataQuery;
import com.ibs.hadoop.hbase.service.run.ExecQueryManger;
import com.ibs.hadoop.hbase.util.IOutils;
import com.ibs.hadoop.hbase.util.PropertiesUtils;

/**
 * 查询
* 源文件名：UserComplaintServiceImpl.java
* 文件版本：1.0.0
* 创建作者：Think
* 创建日期：2016年11月1日
* 修改作者：Think
* 修改日期：2016年11月1日
* 文件描述：TODO
* 版权所有：Copyright 2016 zjhz, Inc. All Rights Reserved.
*/
public class UserComplaintServiceImpl implements UserComplaintService {

    /**
     * 执行查询
    * @字段说明 manaer
    */
    private ExecQueryManger manaer = new ExecQueryManger();

    @Override
    public Map<String, List<UseComplaintBaseClass>> queryUserComplaint(String imsi, String startTime, String endTime,
            List<String> flows) {

        Long timeStart = System.currentTimeMillis();
        // 拼装参数
        QueryBean queryEntry = new QueryBean();

        queryEntry.setImsi(imsi);
        queryEntry.setStartTime(startTime);
        queryEntry.setEndTime(endTime);

        // 获取需要执行的流程
        Map<String, List<UseComplaintBaseClass>> map = null;
        try {
            map = manaer.submitQuery(queryEntry, flows);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Long timeEnd = System.currentTimeMillis();

        // 将查询结果写入文件
        String flag = PropertiesUtils.getInstance().getValue(PropertiesEnumKey.USER_COMPLAINT_WRITE_FLAG.getKey());

        // 如果当前标识是写入文件
        if (PropertiesEnumKey.USER_COMPLAINT_WRITE_FLAG.getValue().equals(flag)) {
            String filePath = PropertiesUtils.getInstance()
                    .getValue(PropertiesEnumKey.USER_COMPLAINT_WRITE_PATH.getKey());

            if (null != filePath) {
                // 首先写查询条件
                StringBuilder msg = new StringBuilder();

                msg.append("query param:");
                msg.append(queryEntry);
                msg.append("\n");
                msg.append("\n");
                msg.append("query time :").append((timeEnd - timeStart) / 1000.0f).append(" second !");
                msg.append("\n");
                msg.append("\n");
                
                filePath = filePath +"queryRsp_"+imsi+"_"+startTime+".txt";

                this.writeFile(map, filePath, msg.toString());
            }
        }

        return map;
    }

    /**
     * 进行文件的写入操作
    * 方法描述
    * @param map
    * @param filePath
    * @创建日期 2016年11月1日
    */
    private void writeFile(Map<String, List<UseComplaintBaseClass>> map, String filePath, String buffMsg) {

        if (null != map && !map.isEmpty()) {

            FileWriter write = null;
            BufferedWriter bufWrite = null;
            try {
                write = new FileWriter(filePath);
                bufWrite = new BufferedWriter(write);

                bufWrite.write(buffMsg);

                Iterator<Entry<String, List<UseComplaintBaseClass>>> iterQuery = map.entrySet().iterator();

                Entry<String, List<UseComplaintBaseClass>> nextValue = null;

                while (iterQuery.hasNext()) {
                    nextValue = iterQuery.next();
                    List<UseComplaintBaseClass> list = nextValue.getValue();

                    bufWrite.write("flow:" + nextValue.getKey()+";size:"+list.size());
                    bufWrite.newLine();
                    for (UseComplaintBaseClass userCompBean : list) {
                        UserComplaintDataQuery iter = UserComplaintFlow.getInstance(nextValue.getKey());

                        // 调用转换方法,将数据转换成行
                        String lineValue = iter.parseLine(userCompBean);

                        bufWrite.write(lineValue);
                        bufWrite.newLine();
                    }
                    bufWrite.newLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOutils.closeStream(bufWrite);
                IOutils.closeStream(write);
            }
        }
    }

    public static void main(String[] args) {
        UserComplaintService service = new UserComplaintServiceImpl();

        List<String> flowList = new ArrayList<>();

        flowList.add("ATTACH");
        flowList.add("DETACH");
        flowList.add("TAU");
        flowList.add("GET");
        flowList.add("POST");
        flowList.add("SND_MMS");
        flowList.add("RCV_MMS");
        flowList.add("DNS");

        Map<String, List<UseComplaintBaseClass>> list = service.queryUserComplaint("999999999999999", "20161102", "20161102",
                flowList);
//        Map<String, List<UseComplaintBaseClass>> list = service.queryUserComplaint("999999999999999", "20161101", "20161101",
//                flowList);

        System.out.println("最终结果:" + list.size());
    }
}
